package org.example.rxJava;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import rx.Observable;

import java.util.concurrent.TimeUnit;

@Slf4j
public class CreaterOperatorDemo {

    /**
     * 单个元素的弹出，不支持数组类型
     */
    @Test
    public void justDemo() {
        Observable.just("hello world")
                .subscribe(s->{
                    log.info(s);
                });

        Observable.just(1,2,3,4)
                .subscribe(s->{
                    log.info(String.valueOf(s));
                });
    }

    @Test
    public void fromDemo() {
        Observable.from(new String[]{"apple", "banana", "pear"})
                .subscribe(s->{
                    log.info(s);
                });
    }

    /**
     * 输出范围内的所有数字
     */
    @Test
    public void rangeDemo() {
        Observable.range(1, 10)
                .subscribe(s -> {
                    log.info(String.valueOf(s));
                });
    }

    @Test
    public void intervalDemo() throws Exception{
        // 每100毫秒打印1次，永不停止
        Observable.interval(100, TimeUnit.MILLISECONDS)
                .subscribe(s -> {
                    log.info(String.valueOf(s));
                });
        Thread.sleep(Integer.MAX_VALUE);
    }

    @Test
    public void deferDemo() {
        // 参数是 observableFactory 工厂，生成主题
        Observable.defer(() -> {
            // 延迟创建
            return Observable.just("hello", "world");
        }).subscribe(s -> {
            log.info(s);
        });
    }

}
